package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AuthorityController")
public class AuthorityController extends CmiEmaController {

	@RequestMapping("/listAuthoritys")
	public ModelAndView listAuthoritys(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = authorityPaginationService.updatePagination(request, pagination);
		Set<Authority> authoritys = authorityPaginationService.getPaginationAuthoritys(pagination);

		mav.addObject("authoritys", authoritys);
		mav.addObject("entityName", "authority");
		mav.addObject("pagination", pagination);
		mav.setViewName("authority/listAuthoritys.jsp");
		return mav;
	}

	public String listAuthoritys() {
		return "redirect:/listAuthoritys";
	}

	@RequestMapping("/viewAuthority")
	public ModelAndView viewAuthority(@RequestParam Integer authorityId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("authority", authorityDAO.findAuthorityByPrimaryKey(authorityId));
		mav.setViewName("authority/viewAuthority.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAuthoritys")
	public ModelAndView confirmDeleteAuthoritys(
			@RequestParam(value = "deleteAuthoritysIds", required = true) Set<Integer> deleteAuthoritysIds) {
		ModelAndView mav = new ModelAndView();

		Set<Authority> deleteAuthoritys = authorityDAO.findAuthoritysByIds(deleteAuthoritysIds);
		Set<Authority> referencedAuthoritys = authorityService.loadReferenceAuthoritys(deleteAuthoritys);
		boolean hasReferenced;
		if (referencedAuthoritys.size() == 0) {
			mav.addObject("deleteEntitys", deleteAuthoritys);
			hasReferenced = false;
		} else {
			deleteAuthoritys.removeAll(referencedAuthoritys);
			mav.addObject("referencedEntitys", referencedAuthoritys);
			mav.addObject("deleteEntitys", deleteAuthoritys);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Authority");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAuthoritys");
		return mav;
	}

	@RequestMapping("/deleteAuthority")
	public String deleteAuthority(@RequestParam Integer authorityId) {
		Authority authority = authorityDAO.findAuthorityByPrimaryKey(authorityId);
		authorityService.deleteAuthority(authority);
		return "forward:/listAuthoritys";
	}

	@RequestMapping("/confirmDeleteAuthority")
	public ModelAndView confirmDeleteAuthority(@RequestParam Integer authorityId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("authority", authorityDAO.findAuthorityByPrimaryKey(authorityId));

		mav.setViewName("authority/deleteAuthority.jsp");

		return mav;
	}

	@RequestMapping("/deleteAuthoritys")
	public String deleteAuthoritys(
			@RequestParam(value = "deleteAuthoritysId", required = true) Set<Integer> deleteAuthoritysId) {
		authorityService.deleteAuthoritys(deleteAuthoritysId);
		return "forward:/listAuthoritys";
	}

	@RequestMapping("/editAuthority")
	public ModelAndView editAuthority(@RequestParam Integer authorityId) {

		ModelAndView mav = new ModelAndView();

		Authority authority = authorityDAO.findAuthorityByPrimaryKey(authorityId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("authority", authority);

		// 把所有的引用authority对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(authorityService.loadAuthorityObjectReferencedSet(authority));

		// 将编辑页面authority添加到mav中
		mav.setViewName("authority/editAuthority.jsp");
		return mav;
	}

	@RequestMapping("/newAuthority")
	public ModelAndView newAuthority() {
		ModelAndView mav = new ModelAndView();
		Authority authority = new Authority();

		mav.addObject("authority", authority);

		// 把所有的引用authority对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(authorityService.loadAuthorityObjectReferencedSet(authority));

		mav.addObject("newFlag", true);
		mav.setViewName("authority/editAuthority.jsp");

		return mav;
	}

	@RequestMapping("/saveAuthority")
	public ModelAndView saveAuthority(@ModelAttribute Authority authority) {

		ModelAndView mav = new ModelAndView();

		Authority existingAuthority = CmiHibernateProxyUtil
				.deproxy(authorityDAO.findAuthorityByPhysicalPrimaryKey(authority.getName()));

		// 新建记录
		if (authority.getId() == null) {
			if (authority.equals(existingAuthority)) {

				mav.addAllObjects(authorityService.loadAuthorityClassReferencedSet(authority));
				// mav = editContinue(mav, authority);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("authority/editAuthority.jsp");
			} else {
				authorityService.saveAuthority(authority);
				mav.setViewName("forward:/listAuthoritys");
			}
		}
		// 编辑记录
		else {
			Authority originAuthority = CmiHibernateProxyUtil
					.deproxy(authorityDAO.findAuthorityByPrimaryKey(authority.getId()));
			if (!authority.equals(originAuthority) && authority.equals(existingAuthority)) {
				mav.addAllObjects(authorityService.loadAuthorityClassReferencedSet(authority));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("authority/editAuthority.jsp");
			} else {
				authorityService.saveAuthority(authority);
				mav.setViewName("forward:/listAuthoritys");
			}
		}
		return mav;
	}

	@RequestMapping("/authorityController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
